package org.csu.iosocket;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by csucoderlee on 2019/6/8
 */
public class Server {

    private static final int PORT = 8000;

    private ServerSocket serverSocket;

    public static void main(String[] args) {
        Server server = new Server(PORT);
        server.start();
    }

    private Server(int port) {
        try {
            this.serverSocket = new ServerSocket(port);
            System.out.println("服务端启动成功，端口:" + port);
        } catch (IOException exception) {
            System.out.println("服务端启动失败");
        }
    }

    private void start() {
        new Thread(() -> doStart()).start();
    }

    private void doStart() {
        while (true) {
            try {
                Socket client = serverSocket.accept();
                new ClientHandler(client).start();
            } catch (IOException e) {
                System.out.println("服务端异常");
                break;
            }
        }
    }
}
